package com.xb.cloud.user.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.util.DateUtils;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
import com.xb.cloud.user.model.SmsSendLog;
import com.xb.cloud.user.model.SmsSendLogExample;
import com.xb.cloud.user.model.mapper.SmsSendLogMapper;
import com.xb.cloud.user.service.client.SmsSendLogService;
import com.xb.cloud.user.service.client.bo.SmsSendLogBo;

@Service
public class SmsSendLogServiceImpl extends AbstractService implements SmsSendLogService {

	@Resource
	private SmsSendLogMapper smsSendLogMapper;

	@Override
	public ServiceReturnInfo createSmsSendLog(SmsSendLogBo record) {
		if (record == null) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PARAM_IS_NULL);
		}
		SmsSendLog smsSendLog = new SmsSendLog();
		BeanUtils.copyProperties(record, smsSendLog);
		smsSendLog.setId(ipKeyGenerator.generateKey());

		smsSendLog.setSendDate(timeService.getDate());
		smsSendLogMapper.insert(smsSendLog);
		return ServiceReturnUtils.success();
	}

	@Override
	public ServiceReturnInfo countByPhoneNumWithDay(long companyId, String phoneNum) {
		SmsSendLogExample example = new SmsSendLogExample();
		String formateOfDay = DateUtils.formateDate(new Date());
		example.createCriteria().andSendDayEqualTo(formateOfDay).andCompanyIdEqualTo(companyId)
				.andPhoneNumEqualTo(phoneNum);
		int count = smsSendLogMapper.countByExample(example);
		return ServiceReturnUtils.success(count);
	}

}
